﻿namespace DynamicChart.Service.FunctionConversion;

public class DateConvertStrategy : IConvertStrategy
{
    private readonly ISqlFunctionAdapter _adapter;

    public DateConvertStrategy(ISqlFunctionAdapter adapter)
    {
        _adapter = adapter;
    }

    public string Convert()
    {
        return Convert("format");
    }
    
    public string Convert(string format)
    {
        return _adapter.DateConvert(":fieldName", format);
    }

    public string GenerateWhereClause(string fieldName, string parameterName, string condition = "=")
    {
        return GenerateWhereClause(fieldName, parameterName,"", condition);
    }

    public string GenerateWhereClause(string fieldName, string parameterName, string format, string condition = "=")
    {
        return Generate(fieldName, "':beginDate'", format,">=") + 
               " and " + 
               Generate(fieldName, "':endDate'", format, "<=");
    }

    private string Generate(string fieldName, string parameterName, string format, string condition = "=")
    {
        string convertedField = _adapter.DateConvert(fieldName, format);
        string formattedValue = _adapter.FormatValue(parameterName);
        return $"{convertedField} {condition} {formattedValue}";
    }
}